# Lab 5 Logistic Regression Classifier
import torch
from torch.autograd import Variable
import numpy as np

torch.manual_seed(777)

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

x_data = np.array([[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]], dtype=np.float32)
y_data = np.array([[0], [0], [0], [1], [1], [1]], dtype=np.float32)

x = Variable(torch.from_numpy(x_data))
y = Variable(torch.from_numpy(y_data))

# model = torch.nn.Sequential(
#     torch.nn.Linear(2, 1),
#     torch.nn.Sigmoid()
# )
model = torch.nn.Sequential()
model.add_module('z', torch.nn.Linear(2, 1))
model.add_module('sigmoid', torch.nn.Sigmoid())
print(model)

opti = torch.optim.SGD(model.parameters(), lr=0.1)

for i in range(2001):
    h = model(x)
    cost = -torch.mean((y * torch.log(h) + (1-y)*torch.log(1-h)))
    cost.backward()
    opti.step()
    opti.zero_grad()
    if i % 100 == 0:
        print(i, cost.data.cpu().numpy())
        predicted = (model(x).data > 0.5).float()
        accuracy = (predicted == y.data).float().mean()
        print(accuracy)